"""
Case Type   : cm_ctl
Case Name   : 资源池化集群执行cm_ctl pause/resume后,执行cm_ctl set/reload
Create At   : 2024/04/05
Owner       : @kyeleze
Description :
    1.执行cm_ctl pause
    2.cm_ctl set/reload
    3.查询集群状态
    4.恢复集群
Expect      :
    1.成功
    2.build失败，合理报错
    3.成功
    4.成功
History     :
"""

import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class CmPauseResumeCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        timeout_res = self.primary_sh.exec_cm_ctl(
            "list", "--param --server|grep agent_network_timeout")
        self.agent_network_timeout = (
            timeout_res.splitlines()[1].strip().split('=')[-1].strip())

    def test_cm(self):
        step = '--step1:执行cm_ctl pause expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('pause')
        self.log.info(result)
        self.assertIn(self.constant.cm_pause_success_msg, result,
                      "执行失败:" + step)

        step = '--step2:cm_ctl set/reload expect:成功--'
        self.log.info(step)
        result = self.sta1_sh.exec_cm_ctl(
            'set', '--param --server -k agent_network_timeout=180')
        self.log.info(result)
        self.assertIn("set cm_server.conf success", result,
                      "执行失败:" + step)
        result = self.sta1_sh.exec_cm_ctl(
            'reload', '--param --server')
        self.log.info(result)
        self.assertIn("reload cm_server.conf success", result,
                      "执行失败:" + step)

        step = '--step3:查询集群状态 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, result,
                      "执行失败:" + step)
        self.assertIn("pausing         : Yes", result,
                      "执行失败:" + step)

        step = '--step5:恢复集群 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('resume')
        self.log.info(result)
        self.assertIn(self.constant.cm_resume_success_msg, result,
                      "执行失败:" + step)
        time.sleep(2)
        result = self.sta1_sh.exec_cm_ctl(
            'set', f'--param --server -k '
                   f'agent_network_timeout={self.agent_network_timeout}')
        self.log.info(result)
        self.assertIn("set cm_server.conf success", result,
                      "执行失败:" + step)
        result = self.sta1_sh.exec_cm_ctl(
            'reload', '--param --server')
        self.log.info(result)
        self.assertIn("reload cm_server.conf success", result,
                      "执行失败:" + step)

    def tearDown(self):
        step = '----恢复环境----'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("resume")
        self.log.info(result)
        self.assertIn(self.constant.cm_resume_success_msg, result,
                      "执行失败:" + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
